package webservices;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {

    private UploadConfig config;
    public void init() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        config = context.getBean("UploadConfig", UploadConfig.class);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");

        // Create path components to save the file
        final String root = config.getUploadPath();
        //String root = getServletContext().getRealPath("/");
        //String fileName = UUID.randomUUID().toString();

        //request.getInputStream()

        //MultipartMap map = new MultipartMap(request, this);
        //String text = map.getParameter("text");
        //File file = map.getFile("file");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print("<form method=\"POST\" enctype=\"multipart/form-data\">" +
                "<input type=\"file\" name=\"file\" /><input type=\"submit\" /></form>");

    }
}
